from py2neo import Graph, Node, NodeMatcher, RelationshipMatcher
import re

graph = Graph("bolt://localhost:7687", username="neo4j", password='1998')
matcher = NodeMatcher(graph)
rmatcher = RelationshipMatcher(graph)
# graph.delete_all()
# disease, kind, alias, brief,
# department, check, method, drug, fee, infect, cure_period, cure_rate, proportion, population, symptom, disease

# people = mather.match('disease')
#
# for p in people:
#     if '' in p['name']:
#         print(p['name'])
#     with open('dict_disease.txt', 'a') as f:
#         print(p['name'])
#         f.write(f'{p["name"]} 1 disease\n')
# print(len(people))
print('查找结点')
# 查找结点
# name = '骨折'
# disease = matcher.match('disease').where(f"_.name='{name}'")
# for jib in disease:
#     print(jib['name'])
print('查找关系')
# 查找关系
# dept=graph.run(f"match(n:department)-[r:dept_contain_disease]->(p:disease) where p.name='{name}' return n,r,p").data()
# for d in dept:
#     print(d['n']['name'], type(d['n']))
#     print(d['p']['brief'])
# cql = f"match(n:department)-[r:dept_contain_disease]->(p:disease) where p.name='{name}' return n"
# departments = graph.run(cql).data()
# for department in departments:
#     print(department['n']['name'])
# 检查项目
# cql = f"match(p:disease)-[r:disease_check]->(n:check) where p.name='{name}' return n"
# datas = graph.run(cql).data()
# for data in datas:
#     print(data['n']['name'])
print('疾病和症状有相同名字的查找')
# 疾病和症状有相同名字的查找
# disease = matcher.match('disease')
# for jib in disease:
#     if matcher.match('symptom').where(f"_.name='{jib['name']}'").first() is not None:
#         print(jib['name'])

print('疾病/症状相关查询')
# 疾病是否拥有disease和symptom两个标签
# with open('disease_and_symptom.txt', 'r') as f:
#     data = f.read()
#     data = data.rstrip('\n').split('\n')
# # name = '头疼'
# # name = '头痛'
# name = '高血压'
# print(name in data)
# disease = matcher.match('disease').where(f"_.name='{name}'").first()
# if disease is not None:
#     print(disease)
#     cql = f"match(p:disease)-[r:disease_drug]->(n:drug) where p.name='{name}' return n"
#     datas = graph.run(cql).data()
#     for data in datas:
#         print(data['n']['name'])
#     # print(disease)
# alias = matcher.match('alias').where(f"_.name='{name}'").first()
# if alias is not None:
#     print(alias['name'])
#     cql = f"match(n:disease)-[r:disease_alias]->(p:alias) where p.name='{name}' return n"
#     disease = graph.run(cql).data()
#     print(disease)
# symptom = matcher.match('symptom').where(f"_.name='{name}'").first()
# if symptom is not None:
#     print(symptom)

print('药物相关查询')
# 药物相关查询
# name = '板蓝根'
# cql = f"match(n:drug) where n.name ='{name}' return n"
# drug = graph.run(cql).data()
# drug = matcher.match('drug').where(f"_.name ='{name}'").first()
# if drug is not None:
#     print(drug)
# else:
#     # cql = f"match(n:drug) where n.name =~'.*{name}.*' return n"
#     # drugs = graph.run(cql).data()
#     # for drug in drugs:
#     #     print(drug['n']['name'])
#     drugs = matcher.match('drug').where(f"_.name =~'.*{name}.*'")
#     for drug in drugs:
#         print(drug['name'])

print('检查项目相关查询')
# 检查项目相关查询
# name = '全血细胞计数'
# check = matcher.match('check').where(f"_.name='{name}'").first()
# if check is not None:
#     print(check['name'])
#     cql = f"match(n:disease)-[r:disease_check]->(p:check) where p.name='{name}' return n"
#     diseases = graph.run(cql).data()
#     for disease in diseases:
#         print(disease['n']['name'])
# else:
#     print('None')
#     checks = matcher.match('check').where(f"_.name=~'.*{name}.*'")
#     print(checks)
#     for check in checks:
#         print(check['name'])
print('常见病')


# 常见病
# cql = f"match(p:kind)-[r:kind_disease]->(n:disease) where p.name='常见病' return n"
# diseases = graph.run(cql).data()
# for disease in diseases:
#     print(disease['n']['name'])
# 人群相关查询
def population():
    print('人群相关查询')
    name = '老人'
    # population = matcher.match('population').where(f"_.name='{name}'").first()
    # if population is not None:
    #     print(population['name'])
    #     cql = f"match(k:kind)-[r1:kind_disease]->(n:disease) " \
    #         f"where k.name='常见病' with n " \
    #         f"match(n:disease)-[r2:disease_population]->(p:population) " \
    #         f"where p.name=~'.*{name}.*' with n " \
    #         f"match(n:disease)-[r:disease_proportion]->(p:proportion) " \
    #         f"return n order by p.name desc LIMIT 25"
    #     diseases = graph.run(cql).data()
    #     for disease in diseases:
    #         print(disease['n']['name'])
    # else:
    populations = matcher.match('population').where(f"_.name=~'.*{name}.*'")
    for population in populations:
        print(population['name'])
    cql = f"match(k:kind)-[]->(n:disease) " \
        f"where k.name='常见病' with n " \
        f"match(n:disease)-[]->(p:population) " \
        f"where p.name=~'.*{name}.*' with n " \
        f"return n order by n.proportion desc LIMIT 25"
    # cql = f"match(n:disease)-[r2:disease_population]->(p:population) " \
    #     f"where p.name=~'.*{name}.*' return n"
    diseases = graph.run(cql).data()
    print(diseases)
    for disease in diseases:
        print(disease['n']['name'])


def method():
    methods = matcher.match('method')
    for method in methods:
        print(method['name'])

def check():
    checks = matcher.match('check')
    for check in checks:
        print(re.sub(r'\\', '', check['name']))

if __name__ == '__main__':
    # check()
    # population()

